#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class InitAddr
{
public:
    InitAddr(struct sockaddr_in addr)
        : _addr(addr)
    {
        //端口号转化
        _port = ntohs(_addr.sin_port);
        //ip转化
        //_ip = inet_ntoa(_addr.sin_addr);
        //这种⬇用的是各自的缓冲空间，是线程安全的
        char ipbuffer[64];
        inet_ntop(AF_INET, &_addr.sin_family, ipbuffer, sizeof(ipbuffer));
        _ip = ipbuffer;
    }

    uint16_t port()
    {
        return _port;
    }

    struct sockaddr_in& NetAddr() 
    {
        return _addr; 
    }

    bool operator==(InitAddr& peer)
    {
        return _port == peer.port() && _ip == peer.ip();
    }

    std::string StringAddr()
    {
        std::string str = _ip + "  " + std::to_string(_port);
        return str;
    }

    std::string ip()
    {
        return _ip;
    }
private:
    struct sockaddr_in _addr;
    uint16_t _port;
    std::string _ip; 
};